package com.example.viewtest.FragmentGroup;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.viewtest.AdapterGroup.Adapter_FragmentTwo;
import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentTwo#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentTwo extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private List<Bean> list = new ArrayList<>();
    private ListView listView;
    private Adapter_FragmentTwo mAdapter;

    public FragmentTwo() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentTwo.
     */
    public static FragmentTwo newInstance(String param1, String param2) {
        FragmentTwo fragment = new FragmentTwo();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_two, container, false);

        initRecyclerViewData();

        listView = view.findViewById(R.id.lv_FragmentTwo);
        //添加头部
        View headListView = inflater.inflate(R.layout.rv_header,container,false);
        listView.addHeaderView(headListView);

        mAdapter = new Adapter_FragmentTwo(requireContext(),list);
        listView.setAdapter(mAdapter);

        return view;
    }
    public void initRecyclerViewData(){
        Random random = new Random();
        for (int i=0;i<30;i++){
            Bean bean = new Bean();
            bean.setName("Item" + i);
//            bean.setType((random.nextInt()*2)+3);//[3,5)
//            Log.d("随机数生成监控","-----: "+bean.getType());
            switch (i%3){
                case 1:
                    bean.setImageId(R.drawable.game1);
                    break;
                case 2:
                    bean.setImageId(R.drawable.game2);
                    break;
                case 0:
                    bean.setImageId(R.drawable.landscape5);
                    break;
                default:
            }
            list.add(bean);
        }
    }
}